<?php


namespace Wyi\WebWechat\extend;


class Helper
{
    /**
     * 取时间戳
     * @param int $length
     * @return int
     * @throws \Exception
     */
    public static function getTimestamp(int $length = 10):int
    {
        if($length === 10){
            return time();
        }

        if ($length === 13){
            list($msec, $sec) = explode(' ', microtime());
            $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
            return (int)substr($msectime,0,13);
        }
        throw new \Exception('length is invalid');
    }

    /**
     * 取中间字符串
     * @param string $str
     * @param string $leftStr
     * @param string $rightStr
     * @return false|string
     */
    public static function getSubStr(string $str, string $leftStr,  string $rightStr)
    {
        $left = strpos($str, $leftStr);
        $right = strpos($str, $rightStr,$left);
        if($left < 0 or $right < $left) return '';
        return substr($str, $left + strlen($leftStr), $right-$left-strlen($leftStr));
    }
}